import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/CommonLoading.dart';
import 'package:mirror/Util.dart';
import 'package:mirror/api/MessageApi.dart';
import 'package:mirror/page/home/pages/message/ChatPage.dart';
import 'package:mirror/page/home/pages/message/ExistGroupPage.dart';
import 'package:mirror/page/home/pages/message/GroupListPage.dart';
import 'package:mirror/widget/EmptyWidget.dart';

class MessagePage extends StatefulWidget {
  const MessagePage({super.key});

  @override
  State<MessagePage> createState() => _MessagePageState();
}

class MessageBean1 {
  final String name;
  final String last_message;
  final String avatar;
  final String timestamp;

  MessageBean1(this.name, this.last_message, this.avatar, this.timestamp);
}

class _MessagePageState extends State<MessagePage> {
  List<MessageBean1> messageList = [
    // MessageBean1("名字", "群消息或对方姓名群消息或对方姓名群消息或对方姓名", "", ""),
    // MessageBean1("名字", "最后一条消息", "", ""),
    // MessageBean1("名字", "最后一条消息", "", ""),
    // MessageBean1("名字", "最后一条消息", "", ""),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Stack(
            children: [
              Container(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  width: double.infinity,
                  color: Constant.mainColor,
                  child: const Center(
                    child: Text(
                      "消息(1假的)",
                      style: TextStyle(color: Colors.white),
                    ),
                  )),
              Positioned(
                  right: 0,
                  bottom: 0,
                  top: 0,
                  child: Center(
                      child: InkWell(
                    onTap: () async {
                      var controller = TextEditingController();

                      int? res = await showDialog(
                          context: context,
                          builder: (c) => AlertDialog(
                                title: const Text("请选择"),
                                content: SingleChildScrollView(
                                  child: Column(
                                    children: [
                                      TextButton(
                                        onPressed: () {
                                          Navigator.pop(context, 0);
                                        },
                                        child: const Text("新建聊天室"),
                                      ),
                                      TextButton(
                                        onPressed: () {
                                          Navigator.pop(context, 1);
                                        },
                                        child: const Text("加入聊天室"),
                                      ),
                                      TextButton(
                                        onPressed: () {
                                          Navigator.pop(context);
                                        },
                                        child: const Text(
                                          "取消",
                                          style: TextStyle(color: Colors.grey),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ));
                      if (res != null) {
                        if (res == 0) {
                          String? res1 = await showDialog(
                              context: context,
                              builder: (c) => SimpleDialog(
                                    title: const Text("新建聊天室"),
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.symmetric(
                                          horizontal: 10,
                                        ),
                                        child: Container(
                                          padding: const EdgeInsets.all(8),
                                          decoration: BoxDecoration(
                                              color: Colors.white,
                                              boxShadow: Constant.boxShadow),
                                          child: TextField(
                                            controller: controller,
                                            maxLength: 30,
                                            decoration:
                                                const InputDecoration.collapsed(
                                                    hintText: "请输入聊天室名称"),
                                          ),
                                        ),
                                      ),
                                      const SizedBox(
                                        height: 8,
                                      ),
                                      Padding(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 8),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.end,
                                          children: [
                                            ElevatedButton(
                                              onPressed: () {
                                                Navigator.pop(context);
                                              },
                                              style: ButtonStyle(
                                                backgroundColor:
                                                    MaterialStateProperty.all(
                                                        Colors.grey),
                                              ),
                                              child: const Text("取消"),
                                            ),
                                            const SizedBox(
                                              width: 10,
                                            ),
                                            ElevatedButton(
                                                onPressed: () {
                                                  if (controller.text.isEmpty) {
                                                    Util.toast("请输入聊天室名称");
                                                    return;
                                                  }
                                                  Navigator.pop(
                                                      context, controller.text);
                                                },
                                                child: const Text("确定")),
                                          ],
                                        ),
                                      )
                                    ],
                                  ));
                          if (res1 == null) {
                            return;
                          }
                          try {
                            CommonLoading.show();
                            await MessageApi.createRoom(roomName: res1);
                            CommonLoading.hide();
                            Util.toast("创建成功");
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (c) => const GroupListPage()));
                          } catch (e) {
                            CommonLoading.hide();
                            print(e);
                          }
                        }
                        if (res == 1) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (c) => ExistGroupPage()));
                        }
                      }
                    },
                    child: const Padding(
                      padding: EdgeInsets.all(8.0),
                      child: Icon(
                        Icons.add,
                        color: Colors.white,
                      ),
                    ),
                  )))
            ],
          ),
          InkWell(
            onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (c) => const GroupListPage()));
            },
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: const [
                  Text("我加入的群聊"),
                  SizedBox(
                    height: 10,
                  ),
                  Icon(Icons.keyboard_arrow_right_outlined)
                ],
              ),
            ),
          ),
          const Divider(),
          Expanded(
            child: messageList.isEmpty
                ? const EmptyWidget()
                : ListView.builder(
                    itemCount: messageList.length,
                    itemBuilder: (c, i) {
                      return Slidable(
                        endActionPane: ActionPane(
                          motion: const ScrollMotion(),
                          children: [
                            SlidableAction(
                              onPressed: (c) {
                                messageList.removeAt(i);
                                setState(() {});
                              },
                              backgroundColor: const Color(0xFFFE4A49),
                              foregroundColor: Colors.white,
                              icon: Icons.delete,
                              label: 'Delete',
                            ),
                          ],
                        ),
                        child: InkWell(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (c) => const ChatPage(
                                          title: '未至名字',
                                          roomId: '',
                                        )));
                          },
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                                vertical: 10, horizontal: 15),
                            child: Row(
                              children: [
                                Expanded(
                                    child: Row(
                                  children: [
                                    Container(
                                      decoration: const BoxDecoration(
                                          color: Colors.blue,
                                          shape: BoxShape.circle),
                                      child: const Padding(
                                        padding: EdgeInsets.all(25),
                                      ),
                                    ),
                                    const SizedBox(
                                      width: 10,
                                    ),
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            messageList[i].name,
                                            maxLines: 1,
                                            style: const TextStyle(
                                                overflow:
                                                    TextOverflow.ellipsis),
                                          ),
                                          Text(
                                            maxLines: 1,
                                            style: const TextStyle(
                                                color: Colors.grey,
                                                overflow: TextOverflow.ellipsis,
                                                fontSize: 12),
                                            messageList[i].last_message,
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                )),
                                const Text("11:00")
                              ],
                            ),
                          ),
                        ),
                      );
                    }),
          )
        ],
      ),
    );
  }

  void refresh() {}
}
